/**
 * ThingDao实现类
 */
package com.zhao.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zhao.condition.Condition;
import com.zhao.dao.inf.ThingDao;
import com.zhao.entity.Thing;
import com.zhao.entity.User;
import com.zhao.enums.StatusEnum;
import com.zhao.enums.ThingTypeEnum;
import com.zhao.utils.PageUtils;

@Repository("thingDao")
public class ThingDaoImpl implements ThingDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void addThing(Thing thing, User user) {
		String sql = "insert into things(thingname,starttime,endtime,thingtype,userid,createtime) values(?,?,?,?,?,?)";
		jdbcTemplate.update(sql, new Object[]{thing.getThingname(),thing.getStarttime(),thing.getEndtime(),thing.getThingtype(),user.getId(),new Date()});
	}

	@Override
	public int getTotalThing(User user, List<Condition> conditions) {
		List<Object> list = new ArrayList<Object>();
		String sql = "select count(userid) from things where userid = ?";
		list.add(user.getId());
		String conSql = this.joinConditions(conditions, list);
		if (!conSql.equals("")) {
			sql = sql + " AND " +conSql;
		}
		return jdbcTemplate.queryForInt(sql, list.toArray(new Object[list.size()]));
	}

	@Override
	public List<Map<String, Object>> getThings(PageUtils pageUtils, User user, List<Condition> conditions) {
		List<Object> list = new ArrayList<Object>();
		String sql = "select id,thingname,starttime,endtime,thingstatus,thingtype from things where userid = ?";
		list.add(user.getId());
		String conSql = this.joinConditions(conditions, list);
		if (!conSql.equals("")) {
			sql = sql + " AND " +conSql;
		}
		sql = sql + " order by createtime desc limit ?,?";
		list.add((pageUtils.getPageNum()-1)*pageUtils.getRows());
		list.add(pageUtils.getRows());
		final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return jdbcTemplate.query(sql, list.toArray(new Object[list.size()]), new RowMapper<Map<String,Object>>() {

			@Override
			public Map<String, Object> mapRow(ResultSet resultSet, int row) throws SQLException {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", resultSet.getObject("id"));
				map.put("thingname", resultSet.getString("thingname"));
				map.put("starttime", dateFormat.format(resultSet.getDate("starttime")).toString());
				map.put("endtime", dateFormat.format(resultSet.getObject("endtime")).toString());
				map.put("thingstatus",StatusEnum.getName(resultSet.getInt("thingstatus")));
				map.put("thingstatusboolean", resultSet.getBoolean("thingstatus"));
				map.put("thingtype", ThingTypeEnum.getName(resultSet.getInt("thingtype")));
				return map;
			}
		});
	}

	@Override
	public void finishThingSingle(int thingid) {
		String sql = "update things set thingstatus = 1 where id = ?";
		jdbcTemplate.update(sql, new Object[]{thingid});
	}
	
	/**
	 * 拼接搜索条件
	 * @return
	 * String
	 * 2015年12月3日
	 */
	public String joinConditions(List<Condition> conditions,List<Object> list) {
		StringBuffer buffer = new StringBuffer("");
		for (int i = 0, len = conditions.size(); i < len; i++) {
			Condition condition = conditions.get(i);
			if (i != 0) {
				buffer.append(" AND ");
			}
			buffer.append(" "+condition.getFieldName());
			if (condition.getRelation().equals("between")) {
				buffer.append(" between ? and ?");
			}else if (condition.getRelation().equals("eq")) {
				buffer.append(" = ?");
			}else if (condition.getRelation().equals("gt")) {
				buffer.append(" > ?");
			}else if (condition.getRelation().equals("lt")) {
				buffer.append(" < ?");
			}else if (condition.getRelation().equals("gte")) {
				buffer.append(" >= ?");
			}else if (condition.getRelation().equals("lte")) {
				buffer.append(" <= ?");
			}else if (condition.getRelation().equals("like")) {
				buffer.append(" like ?");
			}
			list.addAll(condition.getVals());
		}
		
		return buffer.toString();
	}
}
